/**
* Copyright (c) 2004-2013 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

package it.ciano.util;

import java.util.*;

/**
 * The class extends ArrayList and implements a perl like
 * list with the methods push, pop, shift and unshift.
 *
 * @author Luciano Xumerle
 * @version 0.0.1
 */
public class CList extends ArrayList
{

    static final long serialVersionUID = -7322518225212646435L;

    /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public CList()
    {
        super();
    }

    /**
     * Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
     *
     * @param c the collection whose elements are to be placed into this list.
     */
    public CList ( Collection c )
    {
        super ( c );
    }

    /**
     * Constructs an empty list with the specified initial capacity.
     *
     * @param initialCapacity the initial capacity of the list.
     */
    public CList ( int initialCapacity )
    {
        super ( initialCapacity );
    }


    /**
     * Appends the specified element to the end of this list.
     *
     * @param o element to be appended to this list.
     */
    public void push ( Object o )
    {
        this.add ( o );
    }

    /**
     * Appends the specified element to the start of this list.
     *
     * @param o element to be appended to this list.
     */
    public void unshift ( Object o )
    {
        this.add ( 0, o );
    }

    /**
     * Removes and returns the element at the end of this list.
     *
     * @return the last element of this list.
     */
    public Object pop()
    {
        Object yy = this.get ( this.last() );
        this.remove ( this.last() );
        return yy;
    }

    /**
     * Removes and returns the element at the start of this list.
     *
     * @return the first at element of this list.
     */
    public Object shift()
    {
        Object yy = this.get ( 0 );
        this.remove ( 0 );
        return yy;
    }

    /**
     * Returns the index od the last element of this list
     *
     * @return the last element
     */
    public int last()
    {
        return ( this.size() - 1 );
    }

} // end class


